package com.th.coupon.controller;

import com.th.coupon.common.exception.BaseCloudException;
import com.th.coupon.common.vo.CouponTemplateSdk;
import com.th.coupon.common.vo.SettlementInfo;
import com.th.coupon.entity.Coupon;
import com.th.coupon.service.IUserService;
import com.th.coupon.vo.AcquireTemplateRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户操作的控制器
 * @author 涂航
 */
@RestController
public class UserController {


    private final IUserService userService;

    @Autowired
    public UserController(IUserService userService) {
        this.userService = userService;
    }

    /**
     *
     * 根据用户id以及状态查询对应的优惠卷
     * url(127.0.0.1:7002/coupon-distribution/distribution/getCoupons)
     * url(127.0.0.1:10011/discount/coupon-distribution/distribution/getCoupons) 网关API
     * @param userId 用户id
     * @param status 用户状态
     * @return {@link Coupon}
     * @throws BaseCloudException
     */
    @GetMapping("/distribution/getCoupons")
    List<Coupon> findCouponsByStatus(@RequestParam("userId") Long userId, @RequestParam("status") Integer status) throws BaseCloudException{
        return userService.findCouponsByStatus(userId,status);
    }


    /**
     *
     * 根据用户id查询当前可领取的优惠卷模板
     *  url(127.0.0.1:7002/coupon-distribution/distribution/getCouponTemplateSdk)
     * @param userId  用户id
     * @return {@link CouponTemplateSdk}s 优惠卷模板集合
     * @throws BaseCloudException
     */
    @GetMapping("/distribution/getCouponTemplateSdk")
    List<CouponTemplateSdk> findAvailableTemplate(@RequestParam("userId") Long userId) throws BaseCloudException{
        return userService.findAvailableTemplate(userId);
    }

    /**
     * 用户领取优惠卷
     * url(127.0.0.1:7002/coupon-distribution/distribution/acquire)
     * @param acquireTemplateRequest
     * @return {@link Coupon} 优惠卷实体
     * @throws BaseCloudException
     */
    @PostMapping("/distribution/acquire")
    Coupon acquireTemplate(@RequestBody AcquireTemplateRequest acquireTemplateRequest) throws BaseCloudException{
        return userService.acquireTemplate(acquireTemplateRequest);
    }

    /**
     * 结算 核销优惠卷
     * url(127.0.0.1:7002/coupon-distribution/distribution/settlement)
     * @param settlementInfo {@link SettlementInfo}
     * @return {@link SettlementInfo}
     * @throws BaseCloudException
     */
    @PostMapping("/distribution/settlement")
    SettlementInfo settlement(@RequestBody SettlementInfo settlementInfo) throws BaseCloudException{
        return userService.settlement(settlementInfo);
    }

}
